using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RedDotNode
{
    private Dictionary<string, RedDotNode> _nodes = new Dictionary<string, RedDotNode>();
    private RedDotNode _parentNode;
    private int _count;
    private Action<int> _changeCallback;
    public RedDotNode(RedDotNode parent) 
    {
        _parentNode = parent;
    }

    public void AddListen(Action<int> callback) 
    {
        _changeCallback += callback;
    }

    public void RemoteListen(Action<int> callback) 
    {
        _changeCallback -= callback;
    }

    public RedDotNode GetOrAddChild(string key) 
    {
        if (_nodes.TryGetValue(key, out RedDotNode node)) 
        {
            return node;
        }
        var newNode = new RedDotNode(this);
        _nodes.Add(key, newNode);
        return newNode;
    }

    public void ValueIncre(int incre) 
    {
        _count += incre;
        _changeCallback?.Invoke(_count);
        _parentNode?.ValueIncre(incre);
    }

    public int GetCount() 
    {
        return _count;
    }

    public void Destroy() 
    {
        _changeCallback = null;
    }
}
